﻿using System;
using System.Data;
using System.Text;
using CardLogistics.Web.Components;

namespace CardLogistics.Web.MainPage.StaAna.StaAnaCard
{
    public partial class BadCardByBatch : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDLL(ddltypename, "BASIC_BASICTYPE", "TYPENAME", "ID", " and TYPENO=1 and STATUS=0 and ISDELETE=0");  //采购类型
                BindInfo();
            }
        }

        protected void btn_Search_Click(object sender, EventArgs e)
        {
            BindInfo();
        }
        private void BindInfo()
        {
            string ctype = "";
            if (chb_ZK.Checked)
                ctype = "'制卡坏卡',";
            if (chb_WSWT.Checked)
                ctype += "'未售问题卡',";
            if (chb_TH.Checked)
                ctype += "'退换坏卡',";

            StringBuilder sbStr = new StringBuilder();
            sbStr.Append(" select * from (");
            sbStr.Append(" select h.*,r.入库总数,nvl(round(h.坏卡数量/r.入库总数,4),0) 坏卡率,c.SIGNDATE,c.BUYTYPEID from ");
            sbStr.Append(" (select count(0) 坏卡数量,a.conno,a.suppliersname,a.batchno from v_getcardbadinfo a ");
            if (!ctype.Equals(""))
                sbStr.Append(" where a.ctype in(" + ctype.TrimEnd(',') + ")");
            else
                sbStr.Append(" where 1!=1 ");
            sbStr.Append(" group by a.conno,a.suppliersname,a.batchno) h ");
            sbStr.Append(" left join (select sum(b.ACTUALNUMBER) 入库总数,b.batchno from v_storage_detailed_card b where b.issubmit=5 group by b.batchno) r");
            sbStr.Append(" on h.batchno=r.batchno");
            sbStr.Append(" left join purchase_contract c on h.conno=c.conno ");

            sbStr.Append(" ) m where 1=1 ");
            if (txt_batchno.Text.Trim() != "")
                sbStr.Append(" and batchno like '%" + txt_batchno.Text.Trim() + "%'");
            if (txt_StartDATE.Text.Trim() != "")
                sbStr.Append(" and to_char(SIGNDATE,'yyyy-MM-dd')>='" + txt_StartDATE.Text + "' ");
            if (txt_EndDATE.Text.Trim() != "")
                sbStr.Append(" and to_char(SIGNDATE,'yyyy-MM-dd')<='" + txt_EndDATE.Text + "' ");
            if (ddltypename.SelectedIndex > 0)
                sbStr.Append(" and BUYTYPEID='" + ddltypename.SelectedValue + "' ");

            DataTable dt = CardLogistics.DAL.DbHelperSQL.GetQueryResult(sbStr.ToString());
            rptList.DataSource = dt;
            rptList.DataBind();
        }
    }
}